<include file="public@header" />
</head>
<style type="text/css">
    #file-wrap{
        height:34px;
        width: 82px;
        position: relative;
        display: inline-block;
        text-align: center;
        line-height: 34px;
        overflow: hidden;
        font-size: 14px;
        cursor: pointer;
        color: #fff;
        vertical-align: middle;
        background-color: #1a242f;
        border-color: #161f29;
    }
    #file-wrap input{
        position: absolute;
        height: 40px;
        z-index: 2;
        right: 0;
        top: 0;
        filter: alpha(opacity=0);
        -moz-opacity: 0;
        opacity: 0;
        cursor: pointer;
        font-size: 16px;
    }
</style>
<body>
	<div class="wrap">
		<ul class="nav nav-tabs">
            <li><a href="{:url('advert/index')}">广告列表</a></li>
            <li class="active"><a href="{:url('advert/add')}">广告添加</a></li>
		</ul>
		<form method="post" class="form-horizontal js-ajax-form margin-top-20" action="{:url('advert/addpost')}">
			<div class="form-group">
				<label for="input-a_digital" class="col-sm-2 control-label"><span class="form-required">*</span>版本号</label>
				<div class="col-md-6 col-sm-10">
                    <input type="text" class="form-control" id="input-a_digital" name="a_digital" maxlength="4" value="{$version}">
				</div>
			</div>
            <div class="form-group">
                <label for="input-a_title" class="col-sm-2 control-label"><span class="form-required">*</span>广告标题</label>
                <div class="col-md-6 col-sm-10">
                    <input type="text" class="form-control" id="input-a_title" name="a_title" maxlength="16" value="">
                </div>
            </div>
            <div class="form-group">
                <label for="input-a_type" class="col-sm-2 control-label"><span class="form-required">*</span>广告类型</label>
                <div class="col-md-6 col-sm-10" id="input-a_type">
                    <select class="form-control" name="a_type" id="a_type" onchange="status()">
                        <option>请选择</option>
                        <option value="10">启动页</option>
                        <option value="20">直播页</option>
                        <option value="30">其他页</option>
                    </select>
                </div>
            </div>
            <div class="form-group" id="star" style="display: none">
                <label class="col-sm-2 control-label">明星</label>
                <div class="col-md-6 col-sm-10">
                    <input class="form-control" type="text" value="" placeholder="全部明星" onclick="doSelectCategory();" id="js-categories-name-input" readonly/>
                    <input class="form-control" type="hidden" value="0" name="a_udid" id="js-categories-id-input"/>
                </div>
            </div>
            <div class="form-group">
                <label for="input-a_sort" class="col-sm-2 control-label"><span class="form-required">*</span>排序</label>
                <div class="col-md-6 col-sm-10">
                    <input type="number" class="form-control" id="input-a_sort" name="a_sort" min="1" max="100" value="99">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">跳转链接</label>
                <div class="col-md-6 col-sm-10">
                    <input type="url" class="form-control" name="a_url">
                </div>
            </div>
            <div class="form-group">
                <label for="input-a_url" class="col-sm-2 control-label"><span class="form-required">*</span>广告图</label>
                <div class="col-md-6 col-sm-10">
                    <img src="__TMPL__/public/assets/images/default-thumbnail.png" id="img" width="128" style="cursor: pointer"/>
                    <div id="file-wrap">
                        <input type="file" onchange="upload(this)" accept="image/jpg,image/jpeg,image/png" id="input-a_url" name="file" style="display: inline"/>
                        选择图片
                    </div>
                    <input type="button" class="btn btn-default" id="cancel_img" onclick="cancel();" value="取消图片" style="display: none">
                    <input type="hidden" name="path" id="path" value="">
                    <input type="hidden" name="file" id="file" value="">
                    <p class="help-block">头像支持jpg,png,jpeg格式,文件大小最大不能超过1M</p>

                </div>
            </div>
			<div class="form-group">
				<label for="input-a_status" class="col-sm-2 control-label"><span class="form-required">*</span>状态</label>
				<div class="col-md-6 col-sm-10" id="input-a_status">
                    <select class="form-control" name="a_status">
                        <option>请选择</option>
                        <option value="10">启用</option>
                        <option value="20">停用</option>
                    </select>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-offset-2 col-sm-10">
					<button type="submit" class="btn btn-primary js-ajax-submit">添加</button>
				</div>
			</div>
		</form>
	</div>
	<script src="__STATIC__/js/admin.js"></script>
	<script type="application/javascript">

        function status() {
            var options=$("#a_type option:selected");
            if(options.val() == 20){
                $('#star').attr('style','display: block');
            }else{
                $('#star').attr('style','display: none');
                $('#js-categories-name-input').val('');
                $('#js-categories-id-input').attr('value','0');
            }
        }

        function cancel() {
            $('#file-wrap').show();
            $('#input-a_url').val('').attr('style','display: inline');
            $('#img').attr('src','__TMPL__/public/assets/images/default-thumbnail.png');
            $('#cancel_img').attr('style','display: none');
            $('#path').attr('value','');
            $('#file').attr('value','');

        }

        function upload(obj) {
            var $fileinput = $(obj);
            Wind.css("jcrop");
            Wind.use("ajaxfileupload", "jcrop", "noty", function () {
                $.ajaxFileUpload({
                    url: "{:url('advert/uploadImg')}",
                    secureuri: false,
                    fileElementId: "input-a_url",
                    dataType: 'json',
                    data: {},
                    success: function (data, status) {
                        if (data.code) {
                            $("#file-wrap").hide();
                            $('#cancel_img').show();
                            var src  = '{$simgs}'+ data.data.path + data.data.file;
                            $('#img').attr('src',src);
                            $('#path').attr('value',data.data.path);
                            $('#file').attr('value',data.data.file);
                        } else {
                            noty({
                                text: data.msg,
                                type: 'error',
                                layout: 'center',
                                callback: {
                                    afterClose: function () {
                                        reloadPage(window);
                                    }
                                }
                            });
                            cancel();
                        }
                    },
                    error: function (data, status, e) {
                        cancel();
                        alert(e + '\r选择的图片文件错误。请重新选择！');
                    }
                });
            });
            return false;
        }

            function doSelectCategory() {
                var selectedCategoriesId = $('#js-categories-id-input').val();
                openIframeLayer("{:url('gift/stars')}?ids=" + selectedCategoriesId, '请选择明星', {
                    area: ['800px', '500px'],
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        //do something

                        var iframeWin          = window[layero.find('iframe')[0]['name']];
                        var selectedCategories = iframeWin.confirm();
                        if (selectedCategories.selectedCategoriesId.length == 0) {
                            layer.msg('请选择用户');
                            return;
                        }
                        $('#js-categories-id-input').val(selectedCategories.selectedCategoriesId.join(','));
                        $('#js-categories-name-input').val(selectedCategories.selectedCategoriesName.join(' '));
                        //console.log(layer.getFrameIndex(index));
                        layer.close(index); //如果设定了yes回调，需进行手工关闭
                    }
                });
            }
    </script>
</body>
</html>